package com.uottawa.clickerapp.views;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import com.uottawa.clickerapp.domain.*;


import com.uottawa.clickerapp.service.ClickerAppWebServiceClient;
import com.uottawa.clickerapp.service.impl.*;

public class PollsActivity extends Activity {

	private QuestionSession questionSession;
	private Question theQuestion;
	private String session;
	private List<Long> polls;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//d		setContentView(R.layout.activity_polls);
		//ClickerAppWebServiceClient service = ClickerAppWebServiceClient.getInstance();
		//Question currentQuestion = service.getQuestion(keyword);
		Intent fromQuestion = getIntent();
	    String questionText = (String) fromQuestion.getSerializableExtra("QuestionText");
	    session = (String) fromQuestion.getSerializableExtra("session");
        new Thread(new Runnable() {
            public void run() {
            	ClickerAppWebServiceClient client =	ClickerAppWebServiceClientImpl.getInstance();
            	questionSession = client.getQuestions(session);
            }
          }).start();
        try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        theQuestion = null;
        List<Question> questions = new ArrayList<Question>();
        questions = questionSession.getQuestions();
        for (int x = 0; x < questions.size(); x++)
        {
        	if (questions.get(x).getQuestionText().equals(questionText))
        		theQuestion = questions.get(x);
        }
        if (theQuestion != null)
        	loadPolls();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//d		getMenuInflater().inflate(R.menu.activity_polls, menu);
		return true;
	}
	
	protected void loadPolls(){
		
		LinearLayout ll = new LinearLayout(this);
		ll.setOrientation(LinearLayout.VERTICAL);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
		     LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		
		TextView lblQuestion = new TextView(this);
		lblQuestion.setText(theQuestion.getQuestionText());
		ll.addView(lblQuestion, layoutParams);
		
		polls = getResults(theQuestion.getId());
		
		int i = 0;
		for(Answer a: theQuestion.getAnswers()){
			
			TextView ans = new TextView(this);
			ans.setText(a.getAnswerText() + " - " + polls.get(i) + " votes");
			//ProgressBar p = new ProgressBar(this);
			//p.setProgress((int) a.getPolls());
			
			ll.addView(ans, layoutParams);
			//ll.addView(p, layoutParams);
			i++;
		}
		
		setContentView(ll); 
//		new Timer().schedule(new PollReloader(this), 5000, 5000);
		//The timer doesn't work "Only the original thread" that created
		//a view hierarchy can touch its views.
	}
	
	private List<Long> getResults(final long qid){

		 new Thread(new Runnable() {
	            public void run() {
	            	ClickerAppWebServiceClient service = ClickerAppWebServiceClientImpl.getInstance();
	            	polls = service.getResults(qid);
	            }
	          }).start();
	        try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
	        return polls;

	}
	
	
	public Question getQuestion(){
		return theQuestion;
	}
	public void setQuestion(Question q){
		this.theQuestion = q;
	}
	
	

}
